﻿var editWindowModule;
function editmodule_show(path, ref)
{
	if (path)
	{
		if (!ref) ref = '';
		var url = 'editModule.aspx?moduleType=' + path + '&moduleReference=' + ref;
		
		editWindowModule = openpreview(url, 700, 500);
		
		//showBG();
	}
}

function module_render(innerHTML, floatvalue, height, width, padding, margin, border, type, reference)
{
	if (currentEditModule)
	{	
		if (innerHTML && innerHTML != '')
			currentEditModule.innerHTML = innerHTML;
		
		focus();
		if (height == 'mặc định (auto)') height = '';
		if (width == 'mặc định (auto)') width = '';
		if (padding == 'mặc định (0px)') padding = '';
		if (margin == 'mặc định (0px)') margin = '';
		if (border == 'mặc định (none)') border = '';
		
		currentEditModule.style.height = trimstr(height);
		currentEditModule.style.width = trimstr(width);
		currentEditModule.style.padding = trimstr(padding);
		currentEditModule.style.margin = trimstr(margin);
		currentEditModule.style.border = trimstr(border);
		
		floatvalue = trimstr(floatvalue);
		
		if (!document.all)
		{
			currentEditModule.style.cssFloat = floatvalue; // fire fox
		}
		else
		{
			currentEditModule.style.float = trimstr(floatvalue); // ie
		}
		
		
		if (floatvalue == 'none')
		{
			currentEditModule.style.textAlign = 'center';
			currentEditModule.innerHTML = '<center>' + currentEditModule.innerHTML + '</center>';
		}
		currentEditModule.setAttribute('_float', floatvalue);
		
		
		var _type = currentEditModule.getAttribute('_type');
		var _reference = currentEditModule.getAttribute('_reference');
		
		currentEditModule.setAttribute('_type', type);
		currentEditModule.setAttribute('_reference', reference);
		
		currentEditBlock.setAttribute('_type', type);
		currentEditBlock.setAttribute('_reference', reference);
		
		// refresh control
		removeControl(currentEditBlock);
		
		addControlToBlock2(currentEditBlock);
		
		
		
		document.getElementById('customArg').value = type;
		document.getElementById('customArg2').value = reference;
		
		if (type.toLowerCase().indexOf('slideshow5') >= 0)
		{
			slideShow2_directory = '';
			slideShow2_speed = 0;
			slideShow2_postLeft = 0;
			slideShow2_imgThum = document.getElementById('slideShow2_imgThum');
			slideShow2_imgPreview = document.getElementById('slideShow2_imgPreview');
			slideShow2_increaseLeftPost();
			slideShow2_setFirstImage();
		}
		
		if (editWindowModule) editWindowModule.close();
		
	}
}

function editBlock2()
{
	var editorContainer = document.getElementById('editorContainer');
	editorContainer.style.display = 'block';
	editorContainer.style.top = findPosY(currentEditBlock) + 'px';
	editorContainer.style.left = findPosX(currentEditBlock.parentNode) + 'px';
	editorContainer.style.width = document.getElementById('panel').offsetWidth + 'px';
	
	
	var virtualDiv = document.getElementById('virtualDiv');
	if (virtualDiv) virtualDiv.parentNode.removeChild(virtualDiv);
	
	virtualDiv = document.createElement('div');
	virtualDiv.id = 'virtualDiv';
	virtualDiv.innerHTML = '';
	virtualDiv.style.height = blockEditorHeight + 'px';
	currentEditBlock.parentNode.insertBefore(virtualDiv, currentEditBlock);
	
	//window.scrollTo(0, 0);
	//$("#editorContainer").slideToggle("slow");
	
	//document.getElementById('editorContainer').style.height = document.documentElement.scrollHeight;
	//document.getElementById('editorContainer').style.width = (document.documentElement.offsetWidth-24) + 'px';
	
	// find block content
	var span, spans = currentEditBlock.getElementsByTagName('span');
	for (var i=0; i<spans.length; i++)
	{
		span = spans[i];
		if (span.className == 'content') break;
	}
	editor.loadHTML(span.innerHTML);
	
	// bind style
	var		cssFloat = document.all ? currentEditBlock.style.float : currentEditBlock.style.cssFloat,
			height = currentEditBlock.style.height,
			width = currentEditBlock.style.width,
			padding = currentEditBlock.style.padding,
			margin = currentEditBlock.style.margin,
			border = currentEditBlock.style.border;
			
	document.getElementById('Text6').value = height == ''? 'mặc định (auto)' : height;
	document.getElementById('Text7').value = width == '' ? 'mặc định (auto)' : width;
	document.getElementById('Text8').value = padding == '' ? 'mặc định (0px)' : padding;
	document.getElementById('Text9').value = margin == '' ? 'mặc định (0px)' : margin;
	document.getElementById('Text10').value = border == '' ? 'mặc định (none)' : border;
	setValueDropdownlist(document.getElementById('Select2'), cssFloat);
			
	
	
	currentEditBlock.style.display = 'none';
	
			
	if (!currentEditBlock) return false;
	
	//var url = 'blockedit.aspx';
		
	//var editblockform = window.open(url, 'editblockform', 'menubar=0, resizable=0, status=0,scrollbars=1, width=550, height=400', '');
	
	//showBG();
}
function block_cancel()
{
	//$('#editorContainer').slideToggle('slow');
	document.getElementById('editorContainer').style.display = 'none';
	currentEditBlock.style.display = '';
	document.getElementById('virtualDiv').parentNode.removeChild(document.getElementById('virtualDiv'));
	hideBG(); 
}
function block_save()
{
	if (currentEditBlock)
	{
		block_render(document.getElementById("idContent"+editor.oName).contentWindow.document.body.innerHTML,
			document.getElementById('Select2').options[document.getElementById('Select2').selectedIndex].value,
			document.getElementById('Text6').value,
			document.getElementById('Text7').value,
			document.getElementById('Text8').value,
			document.getElementById('Text9').value,
			document.getElementById('Text10').value);
		//$("#editorContainer").slideToggle("slow");
		document.getElementById('editorContainer').style.display = 'none';
		//currentEditBlock.style.height = currentBlockHeight + 'px';
		document.getElementById('virtualDiv').parentNode.removeChild(document.getElementById('virtualDiv'));
		currentEditBlock.style.display = '';
	}
	hideBG();
}

function block_render(innerHTML, floatvalue, height, width, padding, margin, border)
{

	if (currentEditBlock)
	{	
		if (height == 'mặc định (auto)') height = '';
		if (width == 'mặc định (auto)') width = '';
		if (padding == 'mặc định (0px)') padding = '';
		if (margin == 'mặc định (0px)') margin = '';
		if (border == 'mặc định (none)') border = '';
		
		currentEditBlock.style.height = trimstr(height);
		currentEditBlock.style.width = trimstr(width);
		currentEditBlock.style.padding = trimstr(padding);
		currentEditBlock.style.margin = trimstr(margin);
		currentEditBlock.style.border = trimstr(border);
		if (!document.all)
			currentEditBlock.style.cssFloat = trimstr(floatvalue);
		else
			currentEditBlock.style.float = trimstr(floatvalue);
		
		// find block content
		var span, spans = currentEditBlock.getElementsByTagName('span');
		for (var i=0; i<spans.length; i++)
		{
			span = spans[i];
			if (span.className == 'content') break;
		}
		span.innerHTML = innerHTML;
		
	}
}

function module_edit(childControl)
{
	currentEditBlock = childControl.parentNode;
	while (currentEditBlock.className != 'block') currentEditBlock = currentEditBlock.parentNode;
	
	var divs = currentEditBlock.getElementsByTagName('div');
	for (var i=0; i<divs.length; i++)
	{
		currentEditModule = divs[i];
		if (currentEditModule.className == 'module') break;
	}
	
	editmodule_show(currentEditModule.getAttribute('_type'), currentEditModule.getAttribute('_reference'));
}